﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Entity.Models;
using IntelligentParkingLot.Events;
using IntelligentParkingLot.ViewModels.Base;
using IServices;
using Models;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;

namespace IntelligentParkingLot.ViewModels.Pages
{
    public class MenuManagementViewModel : BaseViewModel
    {
        private readonly IRegionManager _regionManager;
        private readonly IMenuService _menuService;
        private readonly IDialogService _dialogService;

        private readonly IEventAggregator _eventAggregator;

        string _searchKey;

        /// <summary>
        ///  刷新命令
        /// </summary>
        public ICommand RefreshCommand { get; set; }

        /// <summary>
        /// 编辑/新增命令
        /// </summary>
        public ICommand EditCommand { get; set; }

        /// <summary>
        /// 删除命令
        /// </summary>
        public ICommand DeleteCommand { get; set; }

        public MenuManagementViewModel(
            IRegionManager regionManager,
            IMenuService menuService,
            IDialogService dialogService,
            IEventAggregator eventAggregator
        )
            : base(regionManager)
        {
            PageTitle = "菜单维护";
            _regionManager = regionManager;
            _menuService = menuService;
            _eventAggregator = eventAggregator;
            Refresh();

            RefreshCommand = new DelegateCommand<string>(Refresh);
            EditCommand = new DelegateCommand<MenuItemModel>(Edit);
            DeleteCommand = new DelegateCommand<MenuItemModel>(Delete);
            _dialogService = dialogService;
        }

        /// <summary>
        ///  编辑/添加方法
        /// </summary>
        /// <param name="model"></param>
        private void Edit(MenuItemModel model)
        {
            IDialogParameters parameters = new DialogParameters();
            parameters.Add("model", model);
            _dialogService.ShowDialog(
                "EditMenuDialogView",
                parameters,
                r =>
                {
                    if (r.Result == ButtonResult.OK)
                    {
                        Refresh(_searchKey);
                        _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
                    }
                }
            );
        }

        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="model"></param>
        private void Delete(MenuItemModel model)
        {
            try
            {
                if (
                    MessageBox.Show("是否确定删除此项？", "提示", MessageBoxButton.YesNo)
                    == MessageBoxResult.Yes
                )
                {
                    // 物理删除
                    _menuService.DeleteById(model.Id);

                    MessageBox.Show("删除完成！", "提示");

                    this.Refresh(_searchKey);

                    _eventAggregator.GetEvent<RefreshMenuEvent>().Publish();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }

        public ObservableCollection<MenuItemModel> Menus { get; set; } =
            new ObservableCollection<MenuItemModel>();
        List<SysMenu> origMenus;

        /// <summary>
        ///  刷新菜单
        /// </summary>
        /// <param name="searchText"></param>
        private void Refresh(string searchText = "")
        {
            _searchKey = searchText;
            Menus.Clear();
            origMenus = _menuService.GetMenuList(searchText);
            FillMenus(Menus, 0);
        }

        private void FillMenus(ObservableCollection<MenuItemModel> menus, int parent_id)
        {
            var sub = origMenus
                .Where(m => m.ParentId == parent_id)
                .OrderBy(o => o.MenuIndex)
                .ToList();

            if (sub.Count() > 0)
            {
                foreach (SysMenu item in sub)
                {
                    MenuItemModel model = new MenuItemModel
                    {
                        Id = item.Id,
                        MenuHeader = item.MenuHeader,
                        MenuIcon = item.MenuIcon,
                        TargetView = item.TargetView,
                        ParentId = parent_id,
                        MenuType = item.MenuType.Value,
                        IsExpanded = true
                    };
                    menus.Add(model);

                    FillMenus(model.Children, item.Id);
                }

                if (parent_id > 0)
                {
                    menus[menus.Count - 1].IsLastChild = true;
                }
            }
        }
    }
}
